<template>
  <div class="list" ref="list">
    <div>
      <div class="aera">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{city}}</div>
          </div>
        </div>
      </div>
      <div class="aera">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="city in hotCities" :key="city.id" @click="changeCurrCity">
            <div class="button">{{city.name}}</div>
          </div>
        </div>
      </div>
      <div class="aera" v-for="(cityArray, key) in cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <ul class="item-list">
          <div
              class="item border-bottom"
              v-for="city in cityArray" :key="city.id"
              @click="changeCurrCity"
          >
            {{city.name}}
          </div>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import BetterScroll from 'better-scroll'
import {mapState, mapMutations} from 'vuex'
export default {
  props: {
    cities: Object,
    hotCities: Array,
    word: String
  },
  computed: {
    ...mapState({
      city: 'city'
    })
  },
  watch: {
    cities () {
      if (this.cities) {
        this.$nextTick(() => {
          this.scroll = new BetterScroll(this.$refs.list, {
            click: true
          })
        })
      }
    },
    word () {
      if (this.word) {
        const el = this.$refs[this.word][0]
        this.scroll.scrollToElement(el)
      }
    }
  },
  methods: {
    ...mapMutations(['changeCity']),
    changeCurrCity (e) {
      this.changeCity(e.target.innerText)
      this.$router.push('/')
    }
  },
  activated () {
    if (this.scroll) {
      this.scroll.scrollTo(0, 0)
    }
  }
}
</script>

<style lang='stylus' rel='stylesheet/stylus' scoped>

  .border-topbottom
    &:before
      border-color #ccc
    &:after
      border-color #ccc
  .border-bottom
    &:before
      border-color #ccc
  .list
    overflow hidden
    position absolute
    top 1.6rem
    left 0
    right 0
    bottom 0
    .title
      line-height .54rem
      background #eee
      padding-left .2rem
      color #666
      font-size .26rem
    .button-list
      overflow hidden
      padding .1rem .6rem .1rem .1rem
      .button-wrapper
        float left
        width 33.33%
        .button
          margin .1rem
          padding .1rem 0
          text-align center
          border .02rem solid #ccc
          border-radius .06rem
    .item-list
      .item
        padding-left .2rem
        line-height .70rem
</style>
